<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="format-detection" content="telephone=no">
        
        <title>搜索可用券</title>
        
        <link rel="stylesheet" href="css/search.css?t=201611301751" />
    </head>
    <body id="coupon" v-cloak>
		<!--搜索栏-->
		<div class="coupon-search">
			<div class="ipt-box">
				<span class="sprites i-search"></span>
				<input id="ipt" class="ipt" type="text" v-model="iptVal" placeholder="输入商品名称快速查找可用券" @keyup.enter="newSearch(iptVal)" />
				<a href="javascript:;" class="sprites i-close" v-show="iptVal!=''" @click="iptVal=''"></a>
			</div>
		</div>
		<!--优惠券列表-->
		<template v-if="goods.length!=0">
		<div class="select-list">
			<div class="select-box">
				<div class="lt">
					<select class="select" v-model="selected" @change="chooseMenu">
						<option value="0">综合排序</option>
						<option value="6">评论数降序</option>
						<option value="8">销售数量降序</option>
						<option value="9">价格升序</option>
						<option value="10">价格降序</option>
					</select>
				</div>
				<div class="rt">
					<a href="javascript:;" @click="chooseSelf"><span class="sprites i-checkbox" :class="{'sel':self==1}"></span><span>苏宁服务</span></a>
					<a href="javascript:;" @click="chooseStock"><span class="sprites i-checkbox" :class="{'sel':stock==1}"></span><span>有货</span></a>
				</div>
			</div>
			<div class="dd" v-infinite-scroll="loadMore()" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
				<div class="item" v-for="item in goods">
					<a href="javascript:;" class="info" @click="goodLink(item)">
						<div class="lt">
							<img :src="item.partnumber | imgURL" alt="" />
						</div>
						<div class="rt">
							<div class="name"><span class="tag">自营</span><span>{{item.catentdesc}}</span></div>
							<div class="price"><span>&yen;{{item.searchPrice}}</span><span class="tag" v-if="item.priceType!=0">{{item.priceType | priceType}}</span></div>
							<div class="comment">
								<p>评论<span>{{item.countOfarticle}}</span>条</p>
								<p>好评<span>{{item.praiseRate}}</span></p>
							</div>
						</div>
					</a>
					<template v-if="item.couponNum!=''">
						<a href="javascript:;" class="view" @click="viewCoupon(item)">
							<p>面额{{item.couponValue}}元，{{item.couponRule}}等{{item.couponNum}}张券可以用</p>
							<p><span>查看更多</span><span class="sprites i-green-arrow"></span></p>
						</a>
					</template>
					<template v-else>
						<a href="javascript:;" class="view">
							<p>此商品没有可用券</p>
						</a>
					</template>
				</div>
			</div>
		</div>
		</template>
		<!--暂无可发的优惠券-->
		<div class="no-data" v-show="onData">
			<div class="icon-nodata"></div>
			<div class="text">搜索不到可用商品</div>
		</div>
		
		<script src="js/zepto.min.js" type="text/javascript"></script>
		<script src="js/vue.min.js" type="text/javascript"></script>
		<script src="js/vue-resource.js" type="text/javascript"></script>
		<script src="js/vue-infinite-scroll.js" type="text/javascript"></script>
		<script src="js/common.js?t=201611301751" type="text/javascript"></script>
		<script src="js/geo.js" type="text/javascript"></script>
		<script type="text/javascript">
			var vm = new Vue({
                el: '#coupon',
                data: {
	                goods: [],
	                commData: [],
	                selected: 0,
	                self:"-1",
	                stock:"-1",
	                totalPage:"",
	                pageNum: 0,
	                cityCode: "",
	                mdmCityCode: "",
	                busy: false,
	                
	                itemsLen:null,
	                sendTimes:"",
	                remainTimes:"",
	                timeRetFlag:"",
	                iptVal: "",
	                retFlag:null,
	                onData: false,
	                
                    err: false,
                    errCtt: ""
                },
                computed: {
                    
                },
                methods: {
                	searchGoods: function(keyword,page){
                    	this.$http.jsonp(DI.searchGoods + "emall/mobile/mobileSearch.jsonp?iv=" + this.stock + "&set=5&keyword="+ keyword +"&cityId=" + this.cityCode + "&ct=" + this.self + "&cp=" + this.pageNum + "&ps=10&st=" + this.selected + "&channelId=WAP", {
							
						}).then(function(res) {
							var result = res.data;
							var resData = [];
							var paramPartnumber = [];
							var commList = [];
							resData = result.goods;
							this.totalPage = Math.ceil(result.goodsCount/10);
							if(resData && resData.length > 0){
								for (var i = 0; i < resData.length; i++) {
									resData[i].couponValue = "";
									resData[i].couponRule = "";
									resData[i].couponNum = "";
									
									resData[i].searchPrice = "";
									resData[i].priceType = "";
									resData[i].praiseRate = resData[i].praiseRate ? resData[i].praiseRate : 1;
							    	this.goods.push(resData[i]);
							    	paramPartnumber.push('000000000' + resData[i].partnumber);
							    }
								
								this.searchPrice(resData, paramPartnumber.join(','),function(data){
									for (var j = 0; j < data.length; j++) {
										commList.push({
								    		actType: data[j],
								    		commCode:'000000000' + resData[j].partnumber,
								    		storeId: resData[j].salesName ? resData[j].salesName : ""
								    	})
									}

									vm.couponCommList(commList,function(data){
										for(var i = 0; i < data.length; i++){
						                    resData[i].couponValue = data[i].couponValue;
						                    resData[i].couponRule = data[i].couponRule;
						                    resData[i].couponNum = data[i].couponNum;
				                		}
									});
								});
							}
							
							if(this.goods.length!=0){
								this.onData = false;
							}else{
								this.onData = true;
							}
							
						}, function(req) {
							
						});
                    },
                   	searchPrice: function(goodsList, params,fun){
                    	this.$http.jsonp(DI.getAllPriceNew + "getAllPriceNew.htm?cmmdtyCode=" + params + "&cityCode=" + this.mdmCityCode + "&chan=5&type=1&isCache=1&callback=FourPage", {
							jsonpCallback: 'FourPage'
						}).then(function(res) {
							var result = res.data;
							var resData = [];
							for(var i = 0; i < goodsList.length; i++){
			                    goodsList[i].searchPrice = result[i].price;
			                    if(result[i].priceType.substring(0,1)!=4){
			                    	goodsList[i].priceType = "0";
								}else{
									goodsList[i].priceType = result[i].priceType;
								}
			                    resData.push(goodsList[i].priceType);
	                		}
							this.commData = resData;
							fun(resData);
						}, function(req) {
			
						});
                    },
                    couponCommList: function(goodsList,fun){
                    	var dataParam = {
                    		cityId: vm.mdmCityCode,
		                    commInfoList: goodsList
                    	}
                    	this.$http.post(DI.queryCouponByCommList, JSON.stringify(dataParam)).then(function(res) {
							var result = res.data;
//							result = {"data":[{"commCode":"000000000183833084","couponNum":1,"couponRule":"全额抵用","couponValue":200.0}],"retFlag":"1"};
							var dataList = [];

							if(result.retFlag!="0") {
								var couponNum,couponRule,couponValue;
								
								for(var i = 0; i < goodsList.length; i++){
									if(result.data.length!=0){
										for(var j = 0; j < result.data.length; j++){
											if(goodsList[i].commCode == result.data[j].commCode){
												couponNum = result.data[j].couponNum;
												couponRule = result.data[j].couponRule;
												couponValue = result.data[j].couponValue;
											}else{
												couponNum="";
												couponRule="";
												couponValue="";
											}
										}
									}else{
										couponNum="";
										couponRule="";
										couponValue="";
									}
									
									dataList.push({
										commCode:goodsList[i].commCode,
										couponNum:couponNum,
										couponRule:couponRule,
										couponValue:couponValue
									});
								}
								fun(dataList);
							}
							
						}, function(req) {
			
						});
                    },
                    viewCoupon: function(item){
                    	location.href = "view_coupon.html?actType=" + item.priceType + "&cityId=" + this.mdmCityCode + "&commCode=000000000" + item.partnumber + "&storeId=" + item.salesName + "&cityCode=" + this.cityCode + "&mdmCityCode=" + this.mdmCityCode;
                   	},
                    chooseMenu: function(){
                    	this.goods = [];
                    	this.pageNum = 0;
                    	this.searchGoods(this.iptVal);
                    },
                    chooseSelf: function(){
                    	this.goods = [];
                    	this.pageNum = 0;
                    	
                    	if(this.self!=1){
                    		this.self = 1;
                    	}else{
                    		this.self = -1;
                    	}
                    	this.searchGoods(this.iptVal);
                    },
                    chooseStock: function(){
                    	this.goods = [];
                    	this.pageNum = 0;
                    	
                    	if(this.stock!=1){
                    		this.stock = 1;
                    	}else{
                    		this.stock = -1;
                    	}
                    	this.searchGoods(this.iptVal);
                    },
                    newSearch: function(keyword){
                    	this.goods = [];
                    	this.pageNum = 0;
                    	this.searchGoods(keyword);
                    	$("#ipt").blur();
                    },
                    goodLink: function(item){
					    if(item.partnumber && item.partnumber != ''){
					    	if(item.priceType && item.priceType == 1){
					    		location.href = '//m.suning.com/product/0000000000/' + item.partnumber + '.html';
					    	}else{
					    		if(item.salesName!=null && item.salesName!=''){
					    			location.href = '//m.suning.com/product/'+ item.salesName +'/' + item.partnumber + '.html';
					    		}else{
					    			location.href = '//m.suning.com/product/0000000000/' + item.partnumber + '.html';
					    		}
					    	}
					    }
                    },
                    loadMore: function() {
                   		if(this.totalPage>1 && this.pageNum!=this.totalPage){
                   			this.busy = true;
							
							this.pageNum++;
							this.searchGoods(this.iptVal,this.pageNum);
						    this.busy = false;
                   		}
    				}
                },
                ready: function(){
                	var urlArgs = Shop.getUrlArgs();
                	this.cityCode = urlArgs.cityCode;
                	this.mdmCityCode = urlArgs.mdmCityCode;
                	this.iptVal = this.iptVal ? this.iptVal : urlArgs.keyword;
                	
//              	this.searchGoods(this.iptVal);
                	$("#ipt").focus();
                }
            });
		</script>
    </body>
</html>